using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class GameManager : MonoBehaviour
{
    private Bird[] birdList;
    private int index = -1;
    private int pigCount;
    private int pigDead;
    private FollowTarget camera;
    public GameOver go;
    public static GameManager Instance { get; private set; }
    private void Awake()
    {
        Instance = this;
       
        pigDead = 0;
    }

    // Start is called before the first frame update
    void Start()
    {
        
        birdList=FindObjectsByType<Bird>(FindObjectsSortMode.InstanceID);
        pigCount= FindObjectsByType<Pig>(FindObjectsSortMode.None).Length; 
        camera=Camera.main.GetComponent<FollowTarget>();
        LoadBird();
        Debug.Log(pigCount);
       
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    public void LoadBird()
    {
        index++;
        if (index <birdList.Length&&pigDead<pigCount)
        {
            
            birdList[index].GoStage(slingshoter.Instance.getcenter());
            camera.SetTarget(birdList[index].transform);
        }
        else
        {
            GameOver();
        }
    }
    public void PigDead()
    {
        pigDead++;
        Debug.Log(pigDead);
        if (pigDead >= pigCount)
        {
            GameOver();
            
        }
    }
    private void GameOver()
    {
        if(pigDead >= pigCount)
        {
            go.Show(3);
        }
        if(pigDead < pigCount&& index >= birdList.Length)
        {
            go.Show(0);
        }
    }

    public void OnReStart()
    {
        SceneManager.LoadScene(0);
    }
    public void LevelList()
    {

    }
}
